package com.atguigu.mybatisplus;

import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlAnnotationIntrospector;

import javax.xml.bind.annotation.XmlSchema;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author zhuzi
 */

public  class JacsonNamespaceInstrospector extends JacksonXmlAnnotationIntrospector
{
	private static final long serialVersionUID = 1L;

	private String getNameSpace(Class c) {
		XmlSchema pkgann = c.getPackage().getAnnotation(XmlSchema.class);
		return pkgann.namespace();
	}

	@Override
	public PropertyName findRootName(AnnotatedClass ac)
	{
		String namespace = getNameSpace(ac.getAnnotated());
		if (ac.getAnnotated() != null)
			return new PropertyName(ac.getAnnotated().getSimpleName(), namespace);
		else
			return super.findRootName(ac);
	}

	@Override
	public PropertyName findNameForSerialization(Annotated a)
	{
		AnnotatedElement ae = a.getAnnotated();
		if (Field.class.isInstance(ae)) {
			Field f = (Field)ae;
			String namespace =  getNameSpace(f.getDeclaringClass());
			if (namespace != null)
				return PropertyName.construct(f.getName(), namespace);
		}

		return super.findNameForSerialization(a);
	}

	@Override
	public String findNamespace(Annotated ann)
	{
		AnnotatedElement ae = ann.getAnnotated();
		if (Method.class.isInstance(ae)) {
			Method m = (Method)ae;
			String namespace = getNameSpace(m.getDeclaringClass());
			if (namespace != null)
				return namespace;

		}
		return super.findNamespace(ann);
	}
}
